package routers

import (
	. "gitee.com/edgebox/metadata/internal/model"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
)

func AddApp(c *gin.Context){
	var json App
	err :=c.BindJSON(&json)
	if err != nil{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:err.Error(),
			Data:nil,
		})
		return
	}
	if err:=json.Insert();err != nil{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:err.Error(),
			Data:nil,
		})
		return
	}else{
		c.JSON(http.StatusOK,reply{
			Code:0,
			Msg:"success",
			Data:nil,
		})
		return
	}
}
func GetApps(c *gin.Context){
	mods := FindApps(&App{})
	if mods != nil && len(mods) > 0{
		c.JSON(http.StatusOK,reply{
			Code:len(mods),
			Msg:"success",
			Data:mods,
		})
		return
	}else{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:"not found",
			Data:nil,
		})
		return
	}
}
func GetAppsStatus(c *gin.Context){
	mods := FindApps(&App{})
	if mods != nil && len(mods) > 0{
		apps := make([]map[string]interface{},len(mods))
		for i:=0; i < len(mods); i++{
			mods[i].Update()
			apps[i] = mods[i].ToMap()
		}
		c.JSON(http.StatusOK,reply{
			Code:len(mods),
			Msg:"success",
			Data:apps,
		})
		return
	}else{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:"not found",
			Data:nil,
		})
		return
	}
}
func GetAppsStatusById(c *gin.Context){
	id:= c.Param("id")
	mods := FindApps(&App{Id:id})
	if mods != nil && len(mods) > 0{
		mods[0].Update()
		c.JSON(http.StatusOK,reply{
			Code:1,
			Msg:"success",
			Data:mods[0].ToMap(),
		})
		return
	}else{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:"not found",
			Data:nil,
		})
		return
	}
}
func UpdateApp(c *gin.Context) {
	var json App
	err :=c.BindJSON(&json)
	if err != nil{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:err.Error(),
			Data:nil,
		})
		return
	}
	if num,err:=json.Save();err != nil{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:err.Error(),
			Data:nil,
		})
		return
	}else{
		c.JSON(http.StatusOK,reply{
			Code:int(num),
			Msg:"success",
			Data:nil,
		})
		return
	}
}
func UpdateAppStatusById(c *gin.Context) {
	id:= c.Param("id")
	bdata,err:=ioutil.ReadAll(c.Request.Body)
	if err != nil{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:err.Error(),
			Data:nil,
		})
		return
	}
	if string(bdata) != "restart"{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:"command error",
			Data:nil,
		})
		return
	}
	mods := FindApps(&App{Id:id})
	if mods != nil && len(mods) > 0{
		if err :=mods[0].Restart();err != nil{
			c.JSON(http.StatusInternalServerError,reply{
				Code:-1,
				Msg:err.Error(),
				Data:nil,
			})
			return
		}else{
			c.JSON(http.StatusOK,reply{
				Code:0,
				Msg:"success",
				Data:nil,
			})
			return
		}
	}else{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:"not found",
			Data:nil,
		})
		return
	}


}
func DelApp(c *gin.Context) {
	id:= c.Param("id")
	var json App
	json.Id = id
	if num,err:=json.Delete();err != nil{
		c.JSON(http.StatusInternalServerError,reply{
			Code:-1,
			Msg:err.Error(),
			Data:nil,
		})
		return
	}else{
		c.JSON(http.StatusOK,reply{
			Code:int(num),
			Msg:"success",
			Data:nil,
		})
		return
	}
}